Ext.define('TS.controller.Menu',{
    extend: 'Ext.app.Controller',
    refs:[
        {ref: 'sysmenu',selector: 'sysmenu'},
        {ref: 'tabPanel',selector:'TStablepanel'}
    ],
   
    stores: ['BaStore'],
    init:function(){
        this.control({
            'sysmenu': {
                itemmousedown: this.loadMenu
            }
        });
        
    },
    loadMenu:function(selModel, record){
	    if (record.get('leaf')) { 
            var panel = Ext.getCmp(record.get('id')); 
            if(!panel){
            	//动态加载Grid
			    /*
			    Ext.require(['Ext.grid.*']);
			    var rowEditing = Ext.create('Ext.grid.plugin.RowEditing',{
			       clicksToMoveEditor: 1,
			       autoCancel: true
			    });
			    
			     panel = Ext.create("Ext.grid.Panel",{
			     store:record.get('stores'),
			     columns:record.get('columns'),
			     errorSummary:false,
			     title: record.get('text'),
			     id:record.get('text')+record.get('id'),
			     columnLines: true,
			     bodyPadding:0,
			     closable: true,
			     bbar: Ext.create('Ext.PagingToolbar', {
				      store: record.get('stores'),
				      displayInfo: true,
				      displayMsg: '显示 {0} - {1} 条，共计 {2} 条',
				      emptyMsg: "没有数据"
			     }),
			     dockedItems: [{
			      xtype: 'toolbar',
			      items: [{
			       text: '增加信息',
			       iconCls: 'icon-add',
			       handler: function(){
				        rowEditing.cancelEdit();
				        var panelStore = this.up("panel").getStore();
				        var panelModel = this.up("panel").getSelectionModel();
				        panelStore.insert(0,panelModel);
				        rowEditing.startEdit(0, 0);
			       }
			      }, '-', {
			       itemId: 'delete',
			       text: '删除信息',
			       iconCls: 'icon-delete',
			       disabled: true,
			       handler: function(){
				        var selection = panel.getView().getSelectionModel().getSelection()[0];
				        var panelStore = this.up("panel").getStore();
				        if (selection) {
				         panelStore.remove(selection);
				        }
			       }
			      }, '-' ,{
			       text:'刷新数据',
			       iconCls:'icon-refresh',
			       handler:function(){
				        var panelStore = this.up("panel").getStore();
				        var currPage = panelStore.currentPage;
				        panelStore.removeAll();
				        panelStore.load(currPage);
			       }
			      }]
			     }],
			     plugins: [rowEditing],
			     listeners: {
			      'selectionchange': function(view, records) {
			         panel.down('#delete').setDisabled(!records.length);
			      }
			     }
			    })*/
			    /*Ext.getCmp("syscontent").add({
					id       : record.raw.id,
					title    : record.raw.text,
					layout   : 'fit',
					autoLoad : {url: '../Extjs4Demo/jsp/test.jsp', scripts:true},
					autoShow : true,
					autoRender : true,
					tabTip   : record.raw.text,
					iconCls  : record.raw.text.iconCls,
					border   : false,
					closable : true
			    });
			    var main = Ext.getCmp("syscontent");
			    panel = main.getComponent(record.get('id'));
				panel.show();
				main.setActiveTab(panel);*/
                panel=Ext.create(record.get('view'),{closable : true});
                var main = Ext.getCmp("syscontent");
                panel.id=record.get('id');
                var p = main.add(panel); 
            	main.setActiveTab(p); 
			    //this.openTab(panel,record.get('id'));
			 }else{
			    var main = Ext.getCmp("syscontent");
			    main.setActiveTab(panel); 
		     }
       }
	 },

    openTab : function (panel,id){ 
        var o = (typeof panel == "string" ? panel : id || panel.id);
        var main = Ext.getCmp("syscontent");
        var tab = main.getComponent(o);      
        if (tab) {
            main.setActiveTab(tab); 
        } else if(typeof panel!="string"){ 
            panel.id = o; 
            var p = main.add(panel); 
            main.setActiveTab(p); 
        }    
    }
});
